#include "junix.h"
using namespace std;

bool valid(string p)
{
	int size = p.size();
	int v=0;
	for(int i=0;i<size;i++)
	{
		if(p[i]=='(')
			++v;
		else 
			--v;

		if(v<0)
			return false;
	}

	return true;
}

int main(int argc, char **argv)
{
	if( argc != 2)
	{
		cerr<<"Usage:"<<argv[0]<<" num"<<endl;
	    exit(1);
	}

	int num =boost::lexical_cast<int>(argv[1]);
	string p(std::string(num,'(')+std::string(num, ')'));

	//first
	cout<<p<<endl;
	while( next_permutation(p.begin(),p.end()))
	{
		if(valid(p))
			cout<<p<<endl;
	}
}
